package edu.scut.course_management.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.core.JsonFactoryBuilder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.scut.course_management.dto.AddCourseDTO;
import edu.scut.course_management.dto.AddNoticeDTO;
import edu.scut.course_management.dto.GetConcreteCourse;
import edu.scut.course_management.dto.GetCourseListDTO;
import edu.scut.course_management.dto.GetListOfStudentsDTO;
import edu.scut.course_management.dto.GetNoticeListDTO;
import edu.scut.course_management.dto.SearchCourseListOfM;
import edu.scut.course_management.dto.SectionsDTO;
import edu.scut.course_management.dto.StudentIdDTO;
import edu.scut.course_management.dto.TeacherIdDTO;
import edu.scut.course_management.exception.CustomException;
import edu.scut.course_management.po.Section;
import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.result.JsonResultFactory;
import edu.scut.course_management.result.ResultCode;
import edu.scut.course_management.service.CourseInfoService;

/**
 * 课程信息管理模块
 * @author thompson
 */
@RestController
@RequestMapping("Course/Monitor")
public class CourseInfoController {

    @Autowired
    private CourseInfoService courseInfoService;

    /**
     * 查询上课学生信息列表
     * URL：http://localhost:8080/Course/Monitor/SearchStudentList
     * POST
     */
    @PostMapping("/SearchStudentList")
    // @PreAuthorize("hasAuthority('teacher')) 疑问：是否需要添加权限？
    public JsonResult getStudentList(@RequestBody SectionsDTO sectionsDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取数据
        List<Section> sections = sectionsDTO.getSections();
        //检查是否为空
        if(sections.size() <= 0){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            try{
                List<GetListOfStudentsDTO> list = courseInfoService.getStudentsList(sections);
                jsonResult = jsonResultFactory.buildSuccessResult(list);   
            } catch(CustomException e){
                jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
            }
        }
        return jsonResult;

    }

    /**
     * 新增/修改课程信息
     * URL：http://localhost:8080//Course/Monitor/AddOrModCourse
     * POST
     */
    @PostMapping("AddOrModCourse")
    @PreAuthorize("hasAuthority('admin')")
    public JsonResult updateCourseInfo(@RequestBody AddCourseDTO addCourseDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        try{
            courseInfoService.updateCourse(addCourseDTO);
            jsonResult = jsonResultFactory.buildSuccessResult();
        }catch(CustomException e){
            jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
        }
        return jsonResult;
    }

    /**
     * 删除课程信息
     * URL: http://localhost:8080/Course/Monitor/DeleteCourse
     * POST
     */
    @PostMapping("DeleteCourse")
    @PreAuthorize("hasAuthority('admin')")
    public JsonResult deleteCourseInfo(@RequestBody SectionsDTO sectionsDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        List<Section> sections = sectionsDTO.getSections();
        if(sections.size() == 0){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            try{
                courseInfoService.DeleteSections(sections);
                jsonResult = jsonResultFactory.buildSuccessResult();
            }catch(CustomException e){
                jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
            }
        }
        return jsonResult;
    }

    /**
     * 返回课程信息列表（管理员）
     * URL: http://localhost:8080/Course/Monitor/GETCourseListOfM
     * POST
     */
    @PostMapping("GETCourseListOfM")
    @PreAuthorize("hasAuthority('admin')")
    public JsonResult getCourseListOfM(@RequestBody SearchCourseListOfM searchCourseListOfM){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
            List<GetCourseListDTO> list = courseInfoService.GetCourseListOfMonitor(searchCourseListOfM);
            jsonResult = jsonResultFactory.buildSuccessResult(list);
        
        

        return jsonResult;
    }

    /**
     * 返回授课信息列表（教师）
     * URL: http://localhost:8080/Course/Monitor/GetCourseListOfT
     * POST
     */
    @PostMapping("GetCourseListOfT")
    public JsonResult getCourseListOfT(@RequestBody TeacherIdDTO teacherIdDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        String teacherId = teacherIdDTO.getTeacherId();
        if(teacherId == null){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            List<GetCourseListDTO> list = courseInfoService.GetCourseListOfTeacher(teacherId);
            jsonResult = jsonResultFactory.buildSuccessResult(list);   
            
        }
        return jsonResult;
    }

    /**
     * 返回上课信息列表（学生）
     * URL: http://localhost:8080/Course/Monitor/GETCourseListOfS
     * POST
     */
    @PostMapping("GETCourseListOfS")
    public JsonResult GetCourseListOfS(@RequestBody StudentIdDTO studentIdDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        String studentId = studentIdDTO.getStudentId();    
        if(studentId == null){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            List<GetCourseListDTO> list = courseInfoService.GetCourseListOfStudent(studentId);
            jsonResult = jsonResultFactory.buildSuccessResult(list);   
        }
        return jsonResult;
    }

    /**
     * 返回某门课程具体信息(学生)
     * URL: http://localhost:8080/Course/Monitor/GETCourseOfS
     * POST
     */
    @PostMapping("GETCourseOfS")
    public JsonResult GetConcreteCourseOfS(@RequestBody SectionsDTO sectionsDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        List<Section> sections = sectionsDTO.getSections();
        if(sections.isEmpty()){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            GetConcreteCourse concreteCourse = courseInfoService.getCourseInfoOfS(sections);
            jsonResult = jsonResultFactory.buildSuccessResult(concreteCourse);
        }

        return jsonResult;
    }

    /**
     * 返回某门课程具体信息(教师)
     * URL: http://localhost:8080/Course/Monitor/GETCourseOfT
     * POST
     */
    @PostMapping("GETCourseOfT")
    public JsonResult GetConcreteCourseOfT(@RequestBody SectionsDTO sectionsDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        List<Section> sections = sectionsDTO.getSections();
        if(sections.isEmpty()){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            GetConcreteCourse concreteCourse = courseInfoService.getCourseInfoOfT(sections);
            jsonResult = jsonResultFactory.buildSuccessResult(concreteCourse);
        }

        return jsonResult;
    }

    /**
     * 返回课程通告（学生）
     * URL: http://localhost:8080/Course/Monitor/GETAnnouncement
     * POST
     */
    @PostMapping("GETAnnouncement")
    public JsonResult GetNoticeList(@RequestBody SectionsDTO sectionsDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        List<Section> sections = sectionsDTO.getSections();
        if(sections.isEmpty()){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            List<GetNoticeListDTO> list = courseInfoService.getCourseNotice(sections);
            jsonResult = jsonResultFactory.buildSuccessResult(list);
        }

        return jsonResult;
    }

    /**
     * 发布课程通告（教师）
     * URL: http://localhost:8080/Course/Monitor/PublishAnnouncement
     * POST
     */
    @PostMapping("PublishAnnouncement")
    public JsonResult PublishNotice(@RequestBody AddNoticeDTO addNoticeDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        //获取需要的数据
        String title = addNoticeDTO.getTitle();
        String content = addNoticeDTO.getContent();
        List<Section> sections = addNoticeDTO.getSections();
        //添加系统时间
        Date dd = new Date();
        //格式化
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sim.format(dd);
        if(content.isEmpty() || sections.isEmpty()|| content==null){
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            try{
                courseInfoService.addCourseNotice(title, content, time, sections);
                List<GetNoticeListDTO> list = courseInfoService.getCourseNotice(sections);
                jsonResult = jsonResultFactory.buildSuccessResult(list);
            }catch(CustomException e){
                jsonResult = jsonResultFactory.buildFailResult(ResultCode.DATABASE_UPDATE_FAIL);
            }
        }
        return jsonResult;
    }

}